﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using W.Common;
using W.Product.Domain.Dto;
using W.Product.Domain.Product;
using W.Product.Read.API.Appliaction.Command.ProductCommand;
using W.Product.Repository.Interface;
using W.Warehouse.Domain;
using W.Warehouse.Repository.Interface;
using static WMSpublic.ParseAPI;


namespace W.Product.Read.API.Appliaction.CommHandler.ProductCommandHandler
{
    public class ProductSelectCommandHandler : IRequestHandler<ProductSelectCommand, ProductPage>
    {

        private readonly IProductRepository _productRepository;
        private readonly IProductTypeRepository _productTypeRepository;

        public ProductSelectCommandHandler(IProductRepository productRepository, IProductTypeRepository productTypeRepository)
        {
            _productRepository = productRepository;
            _productTypeRepository = productTypeRepository;
        }
        /// <summary>
        /// 显示产品管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ProductPage> Handle(ProductSelectCommand request, CancellationToken cancellationToken)
        {
            List<Unit> untis = await GetUnitShow(1, 100);
            List<WarehouseInfo> warehouses = await GetWarehouseShow(1, 100);
            ProductPage page = new ProductPage();
            //List<WarehouseInfo> warehouses =await
            var list = (from a in await _productRepository.GetAll()
                        join b in await _productTypeRepository.GetAll()
                        on a.ProductTypeId equals b.ProductTypeId
                        join c in untis 
                        on a.UnitId equals c.UnitId
                        join d in warehouses on 
                        a.WarehouseId equals d.WarehouseId
                        where a.IsDelete == false
                        select new ProductDto
                        {
                            ProductId = a.ProductId,
                            ProductNo = a.ProductNo,
                            InsideNo = a.InsideNo,
                            ProductName = a.ProductName,
                            ProductDesc = a.ProductDesc,
                            ProductPrice = a.ProductPrice,
                            ProductSpec = a.ProductSpec,
                            ProductTypeId = a.ProductTypeId,
                            ProductWeight = a.ProductWeight,
                            ProductWrap = a.ProductWrap,
                            VendorId = a.VendorId,
                            ManuNo = a.ManuNo,
                            GoOnline = a.GoOnline,
                            Online = a.Online,
                            UnitId = a.UnitId,
                            officeId = a.officeId,
                            ClientId = a.ClientId,
                            WarehouseId = a.WarehouseId,
                            BinLocationId = a.BinLocationId,
                            ProductTypeName = b.ProductTypeName,
                            Price=a.ProductPrice,  
                            UnitName=c.UnitName,
                            WarehouseName=d.WarehouseName,

                        }).ToList();
            if (!string.IsNullOrEmpty(request.ProductNo))
            {
                list = list.Where(x => x.ProductNo == request.ProductNo).ToList();
            }
            if (!string.IsNullOrEmpty(request.ProductName)) {
                list = list.Where(x => x.ProductName.Contains(request.ProductName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.VendorId))
            {
                list = list.Where(x => x.VendorId == request.VendorId).ToList();
            }
            if (!string.IsNullOrEmpty(request.ManuNo))
            {
                list = list.Where(x => x.ManuNo == request.ManuNo).ToList();
            }

            page.tacount = list.Count();
            page.pacount = (int)Math.Ceiling(page.tacount * 1.0 / request.size);
            page.List = list.OrderByDescending(x => x.ProductId).Skip((request.index - 1) * request.size).Take(request.size).ToList();
            return page;
        }


        /// <summary>
        /// 单位
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetUnitShow(int pageIndex, int pageSize)
        {
            var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://10.223.8.18:8000/purchaseRead/api/Unit/Handle?pageIndex=" + pageIndex + "&pageSize=" + pageSize;
            var response = await client.GetAsync(url);
            var json = JsonConvert.DeserializeObject<ApiResultList<Unit>>(response);
            return json.data.list;
        }

        /// <summary>
        /// 仓库类型显示
        /// </summary>
        /// <returns></returns>
        public async Task<dynamic> GetWarehouseShow(int pageIndex, int pageSize)
        {
            var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://10.223.8.18:8000/warehouseRead/api/WarehouseMange/GetWarehouse?pageIndex=" + pageIndex + "&pageSize=" + pageSize; ;
            var response = await client.GetAsync(url);
            var json = JsonConvert.DeserializeObject<ApiResultList<WarehouseInfo>>(response);
            return json.data.list;
        }
        /// <summary>
        /// 单位管理表
        /// </summary>
        public class Unit
        {
            public int UnitId { get; set; }
            /// <summary>
            /// 单位编号
            /// </summary>
            public string? UnitNo { get; set; }
            /// <summary>
            /// 单位名称
            /// </summary>
            public string? UnitName { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? UnitRemarks { get; set; }
            /// <summary>
            /// 公司ID
            /// </summary>
            public string? office_id { get; set; }
            /// <summary>
            /// 用户ID
            /// </summary>
            public int? user_id { get; set; }

        }

        /// <summary>
        /// 仓库管理
        /// </summary>
        public class WarehouseInfo
        {
            public int WarehouseId { get; set; }
            /// <summary>
            /// 仓库名称
            /// </summary>
            public string? WarehouseName { get; set; }
        }

    }
}
